/**
 * Created by xuyq on 2023/3/6.
 */
const Nightmare = require('nightmare');
const nightmare = Nightmare({ show: true });
const url = 'https://movie.douban.com/top250';

const titles = [];
const ratings = [];

const crawlPage = (page) => {
    return nightmare
        .goto(`${url}?start=${page * 25}`)
        .wait('.title')
        .evaluate(() => {
            const titleNodes = document.querySelectorAll('.title');
            const ratingNodes = document.querySelectorAll('.rating_num');
            const titles = [];
            const ratings = [];
            titleNodes.forEach((node) => titles.push(node.innerText));
            ratingNodes.forEach((node) => ratings.push(node.innerText));
            return { titles, ratings };
        })
        .then((result) => {
            titles.push(...result.titles);
            ratings.push(...result.ratings);
        })
        .catch((error) => {
            console.error('Search failed:', error);
        });
};

(async () => {
    for (let i = 0; i < 10; i++) {
        await crawlPage(i);
    }
    console.log({ titles, ratings });
    await nightmare.end();
})();
